package com.hightemperator.controller;

import com.hightemperator.po.Activity;
import com.hightemperator.po.Circle;
import com.hightemperator.po.ReleaseImage;
import com.hightemperator.service.ReleaseService;
import com.hightemperator.util.Constant;
import com.hightemperator.util.LocalResponseBody;
import com.hightemperator.util.UploadUtil;
import com.hightemperator.util.getOfToken;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/api/release")
public class ReleaseController {

    @Autowired
    public ReleaseService releaseService;

    /**
     * 获取发活动时候的标签
     *
     * @return
     */
    @RequestMapping("getLable")
    public LocalResponseBody getLable() {
        return new LocalResponseBody("操作成功").doSuccess(releaseService.getLableList());
    }


    /**
     * 发布活动
     * 里面的lableid 通过 获取上面的标签列表 然后获取里面的id 拼接而成
     * 如果type为1 是更改活动的时候 必传id
     *
     * @param activity
     * @param type     0 添加活动 1 更改活动
     * @return
     */
    @RequestMapping("releaseActivity")
    public LocalResponseBody releaseActivity(HttpServletRequest request,
                                             Activity activity,
                                             @RequestParam(value = "type", defaultValue = "") String type) {
        int requestCode;
        String token = request.getParameter("token");
        Integer uId = getOfToken.getUserMessageString(token, request);
        activity.setReleaseuserid(uId);

        if (type.equals("0")) requestCode = releaseService.addActivity(activity);
        else requestCode = releaseService.updateActivity(activity);

        if (requestCode == 0) return new LocalResponseBody("操作失败").doFail();
        else return new LocalResponseBody("操作成功").doSuccess();
    }

    /**
     * 上传动态的图片
     * @param request
     * @return
     */
    @RequestMapping("releaseCircleImage")
    public LocalResponseBody releaseCircleImage(HttpServletRequest request) {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        ReleaseImage releaseImage = new ReleaseImage();
        if (isMultipart) {
            MultipartFile files = ((MultipartHttpServletRequest) request).getFile("files");
            if (files != null) {
                String tomcatPath = System.getProperty("catalina.home");
                String path = UploadUtil.uploadFileToPath(new MultipartFile[]{files}, tomcatPath, "circlePicture");
                releaseImage.setReleaseImage(UserController.handleShowPhoto("", path, "circlePicture"));
                return new LocalResponseBody("操作成功").doSuccess(releaseImage);
            }
        }
        return new LocalResponseBody("操作失败").doSuccess();
    }

    /**
     * 判断是否可以发布活动
     * @return
     */
    @RequestMapping("getCanRelease")
    public LocalResponseBody getCanRelease(HttpServletRequest request){
        String token = request.getParameter("token");
        Integer releaseId = getOfToken.getUserMessageString(token, request);
        int successCode = releaseService.getCanRelease(releaseId);
        if (successCode == 0){
            return new LocalResponseBody("操作失败").doFail();
        }
        return new LocalResponseBody("操作成功").doSuccess();
    }

    /**
     * 发布动态
     *
     * @param circle
     * @return
     */
    @RequestMapping("releaseCircle")
    public LocalResponseBody releaseCircle(HttpServletRequest request,
                                           Circle circle) {

        String token = request.getParameter("token");
        Integer uId = getOfToken.getUserMessageString(token, request);
        circle.setReleaseid(uId);
        Date date = new Date();
        circle.setCreatetime(date);
        int successCode = releaseService.addCircle(circle);
        if (successCode == 0)
            return new LocalResponseBody("操作失败").doFail();
        return new LocalResponseBody("操作成功").doSuccess();
    }


}